package cn.tedu.csmall.passport.controller;

import cn.tedu.csmall.commons.security.LoginPrincipal;
import cn.tedu.csmall.commons.vo.PageData;
import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.commons.web.ServiceCode;
import cn.tedu.csmall.passport.pojo.param.AdminAddNewParam;
import cn.tedu.csmall.passport.pojo.param.AdminLoginInfoParam;
import cn.tedu.csmall.passport.pojo.param.AdminUpdateParam;
import cn.tedu.csmall.passport.pojo.vo.AdminListItemVO;
import cn.tedu.csmall.passport.pojo.vo.AdminStandardVO;
import cn.tedu.csmall.passport.service.IAdminService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/admins")
@Api(tags = "01.管理员管理模块")
@Slf4j
public class AdminController {
    @Autowired
    private IAdminService adminService;


    // http://localhost:9481/admins/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)

    public JsonResult addNew(AdminAddNewParam adminAddNewParam) {
        log.debug("开始处理[添加管理员]的请求，参数{}", adminAddNewParam);
        adminService.addNew(adminAddNewParam);

        return JsonResult.ok();
    }

    // http://localhost:9481/admins/login
    @PostMapping("/login")
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 50)
    public JsonResult login(AdminLoginInfoParam adminLoginInfoParam){
        log.debug("开始处理[管理员登录]的请求，参数",adminLoginInfoParam);
        String jwt = adminService.login(adminLoginInfoParam);
        return JsonResult.ok(jwt);
    }

    @PostMapping("/delete")
    @ApiOperation("根据ID删除用户")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
    })
    public JsonResult deleteById(
            @Range(min = 1, message = "根据ID删除用户失败，请提交合法的ID值！")
            @RequestParam Long id){
        adminService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @PreAuthorize("hasAnyAuthority('/ams/admin/update')")
    @ApiOperation("根据ID更新用户")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
    public JsonResult updateById(
            @Range(min = 1, message = "根据ID更新用户失败，请提交合法的ID值！")
            @RequestParam Long id,
            @Valid AdminUpdateParam adminUpdateParam){
        adminService.updateInfoById(id, adminUpdateParam);

        return JsonResult.ok();
    }

    @PostMapping("/updatePwd")
    @PreAuthorize("hasAnyAuthority('/ams/admin/update')")
    @ApiOperation("根据ID更新用户密码")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "password", value = "用户密码", required = true, dataType = "String")
    })
    public JsonResult updateById(
            @Range(min = 1, message = "根据ID更新用户失败，请提交合法的ID值！")
            @RequestParam Long id,
            @NotNull(message = "密码不能为空") String password){
        adminService.updatePwdById(id, password);

        return JsonResult.ok();
    }

    @GetMapping("/standard")
    @ApiOperation("根据ID查询用户详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
    })
    public JsonResult getStandardById(
            @Range(min = 1, message = "根据ID查询用户失败，请提交合法的ID值！")
            @RequestParam Long id){
        AdminStandardVO standardById = adminService.getStandardById(id);
        return JsonResult.ok(standardById);
    }

    @GetMapping("/list")
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParam(name = "pageNum", value = "页码值")
    public JsonResult list(
            @Range(min = 1, message = "查询用户列表失败，请提供正确的页码值!") Integer pageNum,
            @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        log.debug("ID，--------------------{}", loginPrincipal.getId());
        log.debug("用户名，--------------------{}", loginPrincipal.getUsername() );

        PageData<AdminListItemVO> pageData = adminService.list(pageNum);
        return JsonResult.ok(pageData);
    }


}
